package game

import (
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"image/color"
)

// game/game.g
const (
	ScreenWidth  = 640 //屏幕宽度
	ScreenHeight = 480 //屏幕高度
	gridSize     = 20  //蛇和食物的尺寸
)

type Game struct {
	snake    *Snake //蛇
	food     *Food  //食物
	gameOver bool   //游戏结束
}

func NewGame() *Game {
	game := &Game{
		snake: NewSnake(), //新建蛇
		food:  NewFood(),  //新建食物
		// gameOver: false, //游戏结束开始是false
	}
	return game //返回游戏
}
func (g *Game) Update() error {
	//开始游戏，控制键盘方向键
	g.snake.Move1()
	g.snake.Move()
	if g.snake.CollidesWithf(g.food) {
		g.snake.Eat()
		g.food.RandomizePosition()
	}
	if g.snake.CollidesWithItself() {
		g.gameOver = true
	}
	if g.snake.CollidesWithBorder(ScreenWidth, ScreenHeight) {
		g.gameOver = true
	}
	return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(color.RGBA{0, 0, 0, 255})
	if g.gameOver { //若游戏结束
		ebitenutil.DebugPrint(screen, fmt.Sprintf("Game over!!!"))
	} else {
		// 清空屏幕，开始游戏
	}
	g.snake.Draw(screen)
	g.food.Draw(screen)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return ScreenWidth, ScreenHeight //窗口大小
}
